<?php

function check_null($val='',$need,$dom=''){
	if(trim($dom)=='' || trim($dom)==NULL){
		if($need){
			return_json($val);
			exit;
		}else{
			return $val;
		}
	}else{
		return $dom;	
	}
}

function return_json($state='',$data='',$timestamp=''){
	$obj->state	= $state;
	switch($state){
		case 0    :$obj->msg='ok!';break;
		case -1   :$obj->msg='error!';break;
		case -10  :$obj->msg='no cid';break;
		case 40001:$obj->msg='Not in line with the current condition of the data!';break;
		case 40002:$obj->msg='miss uid!';break;
		case 40003:$obj->msg='miss project!';break;
		case 40004:$obj->msg='miss nid!';break;
		case 40005:$obj->msg='miss uid!';break;
		case 40006:$obj->msg='miss uid!';break;
		case 40007:$obj->msg='miss content!';break;
		case 40008:$obj->msg='Password not correct!';break;
		case 40010:$obj->msg='The company has been disabled by the administrator permissions!';break;
		case 40012:$obj->msg='miss pwd!';break;
		case 40014:$obj->msg='miss username!';break;
		case 40015:$obj->msg='User not exsit!';break;
		case 40018:$obj->msg='Miss devicetoken!';break;
		case 40019:$obj->msg='Forced cancellated by administrator!';break;
		case 40033:$obj->msg='miss uuid!';break;
		case 40034:$obj->msg='miss major_id!';break;
		case 40035:$obj->msg='miss minor_id!';break;
		case 40036:$obj->msg='miss file';break;
		case 40037:$obj->msg='miss duration';break;
		case 40038:$obj->msg='miss cid!';break;//公司id
		case 40039:$obj->msg='miss conditions!';break;//公司id
		case 40040:$obj->msg='The email format is not correct!';break;//公司id
		case 40041:$obj->msg='The tel format is not correct!';break;//公司id
		case 40042:$obj->msg='Miss timestamp!';break;
		case 40043:$obj->msg='Miss event_name!';break;
		case 40044:$obj->msg='Miss event_startTime!';break;
		case 40045:$obj->msg='Miss event_endTime!';break;
		case 40046:$obj->msg='Miss event_execution_id!';break;
		case 40047:$obj->msg='Miss event_examination_id!';break;
		case 40048:$obj->msg='Miss event_acceptance_id!';break;
		case 40049:$obj->msg='Miss project_id!';break;
		case 40050:$obj->msg='Miss event_id!';break;
		case 40051:$obj->msg='Miss leave_startTime!';break;
		case 40052:$obj->msg='Miss leave_endTime!';break;
		case 40053:$obj->msg='Miss leave_type!';break;
		case 40054:$obj->msg='Miss leave_uid!';break;
		case 40055:$obj->msg='Miss leave_id!';break;
		case 40056:$obj->msg='Miss leave_state!';break;
		case 40057:$obj->msg='Miss meeting_title!';break;
		case 40058:$obj->msg='Miss meeting_startTime!';break;
		case 40059:$obj->msg='Miss meeting_endTime!';break;
		case 40060:$obj->msg='Miss meeting_local!';break;
		case 40061:$obj->msg='Miss meeting_join!';break;
		case 40062:$obj->msg='Miss meeting_id!';break;
		case 40063:$obj->msg='Miss back_time';break;
		case 40064:$obj->msg='Miss mobiletype';break;
		case 40065:$obj->msg='Miss app_for';break;
		case 40066:$obj->msg='Miss version';break;
		case 40067:$obj->msg='Miss app_openUrl';break;
		case 40068:$obj->msg='Miss imUsernameArray';break;
		case 40069:$obj->msg='Miss local_id';break;
		case 40070:$obj->msg='Miss building_id';break;
		case 40071:$obj->msg='Miss floor_id';break;
		case 40072:$obj->msg='Miss searchType';break;
		case 40073:$obj->msg='The message can\'t send,please try again!';break;
		case 40074:$obj->msg='The meeting has been cancelled';break;
		case 40075:$obj->msg='Miss log_id';break;
		case 40076:$obj->msg='Miss meeting_room_id';break;
		case 40077:$obj->msg='The meeting_room has been scheduled by other!';break;
		case 40078:$obj->msg='Miss meeting_date';break;
		case 40079:$obj->msg='Miss event_state';break;
		case 40080:$obj->msg='Miss audience_id';break;
        case 40081:$obj->msg='Miss project_name';break;
        case 40082:$obj->msg='Miss project_description';break;
        case 40083:$obj->msg='Miss project_startTime';break;
        case 40084:$obj->msg='Miss project_endTime';break;
        case 40085:$obj->msg='Miss project_leader';break;
        case 40086:$obj->msg='Miss sd_type';break;
        case 40087:$obj->msg='Miss sd_title';break;
        case 40088:$obj->msg='Miss sd_detail';break;
        case 40089:$obj->msg='Miss sd_id';break;
        case 40090:$obj->msg='repeat apply!';break;
        case 40091:$obj->msg='Miss state';break;
        case 40092:$obj->msg='Miss local';break;
		case 40093:$obj->msg='Miss have_answer';break;
		case 40094:$obj->msg='Miss questionnaire_id';break;
		case 40095:$obj->msg='Miss answers';break;
		case 40096:$obj->msg='Miss title';break;
	}
	if($timestamp!='')
		$obj->timestamp=$timestamp;
	if($data!='')
		$obj->data=$data;
	echo json_encode($obj);
}

function Database($cid){
	if(!isset($cid)){
		return_json(40038);
		exit();
	}else{
		$rs=M('Company','','')->db(1,'DB_CONFIG1')->where('company_id='.$cid)->getField('company_disabled');
		if(!$rs){
			return_json(40010);
			exit();	
		}
		$cid = (int)$cid;
		$da=C('DOMAIN_TAG_NAME')."_".$cid;
		C('DB_MEETING.DB_NAME',$da);
		return true;
	}
}

//发送短信
function for_sms($phone_no, $content){
	$flag = 0;
	$argv = array(
		'sn'      =>'SDK-BBX-010-15319',
		'pwd'     =>strtoupper(md5('SDK-BBX-010-15319'.'b5201+8+')),
		'mobile'  =>$phone_no,
		'content' =>iconv("UTF-8","gb2312//IGNORE",$content.'【奇步互动】'),
		'ext'     =>'',
		'stime'   =>'',
		'rrid'    =>''
	);
	foreach ($argv as $key=>$value){
		if ($flag!=0){
		$params .= "&";
		$flag    = 1; 
		}
		$params .= $key."=";
		$params .= urlencode($value);
		$flag = 1;
	}
	$length = strlen($params);
	$fp     = fsockopen('sdk105.entinfo.cn',8060,
				$errno,$errstr,10) or exit($errstr."--->".$errno);
	$header = "POST /webservice.asmx/mt HTTP/1.1\r\n";
	$header .= "Host:sdk105.entinfo.cn\r\n";
	$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
	$header .= "Content-Length: ".$length."\r\n";
	$header .= "Connection: Close\r\n\r\n";

	$header .= $params."\r\n";
	fputs($fp,$header);
	$inheader= 1;
	while (!feof($fp)){
		$line = fgets($fp, 1024);
		if ($inheader && ($line == "\n" || $line == "\r\n")){
		$inheader = 0;
		}
		if ($inheader == 0){
		//echo $line;
		}
	}
	$line = str_replace("<string xmlns=\"http://tempuri.org/\">","",$line);
	$line = str_replace("</string>","",$line);
	$result = explode("-",$line);
	if (count($result) > 1)
		return false;
	else
		return true;
}

//发邮件
function for_mail($email,$title,$content){
	$_POST['timestamp']=time();
	include_once("Public/PHPMailer_v5.1/class.phpmailer.php");
	$mail = new PHPMailer();
	$address =	$email;
	$mail->IsSMTP();
	$mail->Host = "smtp.exmail.qq.com";
	$mail->SMTPAuth = true;
	$mail->Username = "support@qeebu.cn";
	$mail->Password = "zhanghao123";
	$mail->Port=25;
	$mail->From = "support@qeebu.cn";
	$mail->FromName = "";
	$mail->AddAddress("$address", "");
	$mail->AddReplyTo("", "");
	$mail->IsHTML(true); 
	$mail->CharSet = "UTF-8"; 
	$mail->Subject = $title; 
	$mail->Body = $content; 
	if(!$mail->Send())
		return false;
	else
		return true;
}

//创建sqlite数据库
function make_task_sqlite(){
	try{
		$path='Public/Uploads/sqlite/init/task.db';
		
		$dbh=new PDO("sqlite:{$path}");
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
		$sql .= "CREATE TABLE user(
			user_id integer PRIMARY KEY,
			user_username Varchar DEFAULT NULL,
			user_password Varchar DEFAULT NULL,
			user_nickname Varchar DEFAULT NULL,
			user_avatar_big Varchar DEFAULT NULL,
			user_avatar_small Varchar DEFAULT NULL,
			user_vip integer DEFAULT NULL
		);";
		
		$sql .= "CREATE TABLE ppt(
			ppt_id integer PRIMARY KEY,
			ppt_title Varchar DEFAULT NULL,
			ppt_image_id Varchar DEFAULT NULL,
			ppt_image_name Varchar DEFAULT NULL,
			ppt_ttf_bg integer DEFAULT 0,
			ppt_ttf_color Varchar DEFAULT NULL,
			ppt_ttf_style Varchar DEFAULT NULL,
			ppt_ef_style Varchar DEFAULT NULL,
			ppt_sort integer DEFAULT 0
		);";
		$dbh->exec($sql);
	}catch(Exception $e) {
		echo "error!!:$e";
		exit;
	}		
}
?>